<template>
<el-container>
  <div class="row-header">
    <el-form :model="form">
      <el-row>
        <el-col class="box" :xl="{ span: 6 }" :lg="{ span: 6 }">
          <el-form-item label="套餐名称" label-width="80px">
            <el-input size="small" v-model="mealName" style="width: 95%;"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="box" :xl="{ span: 6 }" :lg="{ span: 6 }">
          <el-form-item label="请选择套餐类型">
            <el-select v-model="dictMealType" placeholder="请选择套餐组类型" style="95%">
              <el-option v-for="t in dictMealTypeForSearch" :key="t.id" :label="t.name" :value="t.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col class="box" :xl="{ span: 6}" :lg="{ span: 6}">
          <el-form-item label-width="80px">
            <el-button-group>
              <el-button icon="el-icon-news" size="small" @click="currentPage = 0 ; initData()">查询</el-button>
            </el-button-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col class="left" :xl="{ span: 6, offset:12 }" :lg="{ span: 6, offset:12 }">
          <el-form-item label-width="80px">
            <el-button-group>
              <el-button icon="el-icon-news" size="small">导出EXCEL</el-button>
            </el-button-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
  <div class="split-line"></div>
  <div class="row-main">
    <el-table :header-cell-style="{ 'background':$store.state.app.tableBgColor,'color':'#606266' }" :data="tableData" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange" v-loading.fullscreen="" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0.2)">
      <el-table-column prop="mealName" label="套餐名称" align="center"></el-table-column>
      <el-table-column prop="dictMealTypeName" label="套餐类型" align="center"></el-table-column>
      <el-table-column prop="dictSetMealGroupTypeName" label="套餐组类型" align="center"></el-table-column>
      <el-table-column prop="name" label="所属组" align="center"></el-table-column>
      <el-table-column prop="officialPrice" label="官方售价" align="center"></el-table-column>
      <el-table-column prop="mealSpecConverted" label="规格" align="center"></el-table-column>
      <el-table-column prop="terOfValidity" label="有效期" align="center"></el-table-column>
      <el-table-column prop="ceilingPrice" label="代理设定的上限价格" align="center"></el-table-column>
      <el-table-column prop="salePrice" label="己方售价" align="center"></el-table-column>
      <el-table-column prop="dictMonthlyPaymentName" label="是否分月到账" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-dropdown trigger="click">
            <div>
              <svg-icon class="el-dropdown-link" icon-class="operation"></svg-icon>
            </div>
            <el-dropdown-menu slot="dropdown">
              <el-button class="operation" style="width: 100%;" @click="setSalePrice(scope.row)">设置套餐售价</el-button>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div :style="{'overflow-x': $store.state.app.screenWidth<992?'scroll':'none'}">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="currentPage" :page-size="10" layout="prev, pager, next, jumper" :page-count="maxPage" :hide-on-single-page="value" background></el-pagination></div>
</el-container>

</template>

<script>
  import {setMealListForUser, setSalePriceForUser} from '@/api/getData'

  export default {
    name: "rowOneIndex",
    data() {
      return {
        form: {},
        value: false,
        maxPage: 1,
        tableData: [],
        pageSize: 10,
        currentPage: 1,
        tableLoading: false,
        headerId: '',
        dictMealType:'',
        mealName:'',
        dictMealTypeForSearch: [
            {
              id:'',
              name:'请选择'
            },
            {
                id: 'SETMEAL_ADD_OIL',
                name: '加油包套餐'
            },
            {
                id: 'SETMEAL_BASE',
                name: '基础包套餐'
            }
        ]
      }
    },
    created() {
      this.initData()
    },
    methods: {
      async initData() {
        const returnData = await setMealListForUser({
            mealName:this.mealName,
            dictMealType:this.dictMealType,
            page: {
              current: this.currentPage + '',
              size: this.pageSize + '',
              ascs: this.ascs,
              descs: this.descs
            }
          },
          {
            obj: this,
            key: 'tableLoading'
          })
        this.tableData = []
        if (returnData._result) {
          this.maxPage = Math.ceil(returnData.data.total / this.pageSize) // 分页的最大页数
          returnData.data.records.map(item => {
            const obj = {}
            obj.id = item.id
            obj.nickName = item.nickName // 所属客户
            obj.mealName = item.mealName
            obj.dictMealTypeName = item.dictMealTypeName
            obj.dictSetMealGroupTypeName = item.dictSetMealGroupTypeName
            obj.officialPrice = item.officialPrice + "元"
            obj.ceilingPrice = item.ceilingPrice + "元"
            obj.dictMealType = item.dictMealType
            obj.terOfValidity = item.terOfValidity + "个月"
            obj.mealSpec = item.mealSpec + 'MB'
            obj.dictMonthlyPaymentName = item.dictMonthlyPaymentName
            obj.feeRateId = item.feeRateId
            obj.salePrice = item.salePrice
            obj.costPrice = item.costPrice
            if (item.terOfValidity == null) {
              obj.terOfValidity = "无";
            }
            if(item.mealSpec >= 1024){
                obj.mealSpecConverted = item.mealSpec/1024+"GB"
            } else {
                obj.mealSpecConverted = item.mealSpec+"MB"
            }
            obj.mealSpec = item.mealSpec
            obj.name = item.name
            this.tableData.push(obj)
          })
          this.tableLoading = false
        }
      },
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      handleSizeChange(val) {
        //console.log(`每页 ${val} 条`)
        this.pageSize = val
      },
      handleCurrentChange(val) {
        //console.log(`当前页: ${val}`)
        this.currentPage = val
        this.initData()
      },
      async setSalePrice(obj) {
        this.$prompt('请输入销售价格', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            inputPattern: /([1-9][0-9]*(\.\d{1,2})?)|(0\.\d{1,2})/,
            inputErrorMessage: '请输入整数',
            inputValue:obj.salePrice
        }).then(async ({ value }) => {
            let a = await setSalePriceForUser({
                id:obj.id,
                feeRateId:obj.feeRateId,
                salePrice:value
            });
            if(a._result){
                this.$message.success("操作成功！")
                this.initData();
            }
        }).catch(() => {
            this.$message({
                type: 'info',
                message: '取消输入'
            });
        });
      }
    }
  }
</script>

<style scoped lang="scss">
  .topRow{
    display: flex;
    flex-direction: row;
    justify-content: space-between;
  }
  .el-popper[x-placement^=bottom] {
    margin-right: -31px;
    box-shadow: 0px 0px 10px #888888;
    margin-left: 6px;

    .operation {
      border: 0;
      outline: none;
      display: block;
      text-align: center;
      margin: 0;
    }
  }

  .el-popper[x-placement^=top] {
    box-shadow: 0px 0px 10px #888888;
    margin-left: 6px;
    margin-right: -31px;

    button {
      display: block;
    }

    .operation {
      border: 0;
      outline: none;
      display: block;
      text-align: center;
      margin: 0;
    }
  }
</style>
